<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/24
 * Time: 4:14 PM
 */
namespace app\backend\modules\sys\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\sys\services\CarouselService;
use app\common\models\CarouselItem;
use app\common\exceptions\BizException;

class CarouselItemController extends BaseController
{
    /**
     * 列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        return view("backend.sys.carouselItem.index");
    }

    /**
     * 列表数据
     */
    public function getList(){
        $search = request()->search;
        if(!$search['carousel_id']){
            throw new BizException('请传入幻灯片组ID');
        }
        $list = (new CarouselService())->getItemList($search);
        return $this->successJson('',$list);
    }

    /**
     * 详情页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail(){
        return view("backend.sys.carouselItem.detail");
    }
    public function create(){
        $data = request()->params;
        $data['site_id'] = \YunCms::siteId();
        $data['display_order'] = intval($data['display_order']);
        $model = new CarouselItem();
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $model->save();
            return $this->successJson();
        }
    }
    public function update(){
        $data = request()->params;
        $id = $data['id'];
        $model = CarouselItem::find($id);
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        $data['display_order'] = intval($data['display_order']);
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $model->save();
        }
        return $this->successJson();
    }
    /**
     * 详情数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDetail(){
        $id = request()->id;
        $model = CarouselItem::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            return $this->errorJson('未找到数据');
        }
        return $this->successJson('',$model);
    }
    public function changeStatus()
    {
        $id = request()->id;
        $model = CarouselItem::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        $status=CarouselItem::STATUS_NORMAL;
        if($model->status==CarouselItem::STATUS_NORMAL){
            $status= CarouselItem::STATUS_BAN;
        }
        $model->status = $status;
        $model->save();
        return $this->successJson();
    }
    /**
     * 删除
     */
    public function delete(){
        $id = request()->id;
        $model = CarouselItem::find($id);
        if(!$model){
            return $this->errorJson('未找到数据');
        }
        $model->delete();
        return $this->successJson('');
    }
}